home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagn_r.zip / NUMBERS.SWG / 0066_re: Help With Binary Bits.pas < prev    next >
Pascal/Delphi Source File  |  1995-02-28  |  1KB  |  59 lines

  1.  
  2.  { This code is untested, but should work correctly }
  3.  
  4.  { A word is arranged like this...                     }
  5.  
  6.  { The Word   -  0  0  0  0  0  0 0 0 0 0 0 0 0 0 0 0  }
  7.  { Bit Number - 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0  }
  8.  
  9. Const
  10.  Bit0 = 1;
  11.  Bit1 = 2;
  12.  Bit2 = 4;
  13.  Bit3 = 8;
  14.  Bit4 = 16;
  15.  Bit5 = 32;
  16.  Bit6 = 64;
  17.  Bit7 = 128;
  18.  
  19.  Bit8 = 256;
  20.  Bit9 = 512;
  21.  Bit10 = 1024;
  22.  Bit11 = 2048;
  23.  Bit12 = 4096;
  24.  Bit13 = 8192;
  25.  Bit14 = 16384;
  26.  Bit15 = 32768;
  27.  
  28. Procedure SetBit(SetWord, BitNum : Word);
  29.  Begin
  30.   SetWord := SetWord Or BitNum;     { Set bit }
  31.  End;
  32.  
  33. Procedure ClearBit(SetWord, BitNum : Word);
  34.  Begin
  35.   SetWord := SetWord Or BitNum;     { Set bit    }
  36.   SetWord := SetWord Xor BitNum;    { Toggle bit }
  37.  End;
  38.  
  39. Procedure ToggleBit(SetWord, BitNum : Word);
  40.  Begin
  41.   SetWord := SetWord Xor BitNum;    { Toggle bit }
  42.  End;
  43.  
  44.  Function GetBitStat(SetWord, BitNum : Word) : Boolean;
  45.   Begin
  46.    If SetWord And BitNum = BitNum Then            { If bit is set }
  47.     GetBitStat := True Else GetBitStat := False;
  48.   End;
  49.  
  50.  
  51.  
  52.  SetWord is the word that contains the bit you want to set.  BitNum is
  53.  the number of the bit you want to set as defined in the const section
  54.  (Bit0, Bit1, etc...). GetBitStat returns true if the bit is Set,
  55.  otherwise it returns false.
  56.  
  57.                                                         -Rick
  58.  
  59.